<template>
  <div>
    <div>
      <el-select
        v-model="request.processStatus"
        size="mini"
        style="width: 200px; margin-right: 20px"
        placeholder="告警状态"
        clearable
        filterable
        @change="drawLine()"
      >
        <el-option v-for="item in processStatus" :key="item.code" :label="item.desc" :value="item.code"></el-option>
      </el-select>
      <el-select
        v-model="request.level"
        size="mini"
        style="width: 200px; margin-right: 20px"
        placeholder="告警等级"
        clearable
        filterable
        @change="drawLine()"
      >
        <el-option v-for="item in levels" :key="item.code" :label="item.desc" :value="item.code"></el-option>
      </el-select>
      <el-date-picker
        v-model="dateValue"
        type="datetimerange"
        format="yyyy-MM-dd HH:mm:ss"
        value-format="yyyy-MM-dd HH:mm:ss"
        :picker-options="datePickerConfig"
        range-separator="至"
        start-placeholder="记录开始时间"
        end-placeholder="记录结束时间"
        align="right"
        :default-time="['00:00:00', '23:59:59']"
        style="margin-right: 20px"
        @change="drawLine()"
      ></el-date-picker>
    </div>
    <div style="margin-top: 10px; padding: 10px">
      <el-card>
        <div id="myChart" :style="{ width: '800px', height: '600px' }"></div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRecordCountByProcessStatus } from '@/api/alarm/record'
import store from '@/store'
import { serverGetAll } from '@/api/alarm/server'

export default {
  name: 'RecordEcharts',
  data() {
    return {
      request: {
        startTime: '',
        endTime: '',
        processStatus: 2,
        level: 1
      },
      dateValue: '',
      processStatus: store.getters.processStatus,
      levels: store.getters.levels,
      servers: []
    }
  },
  computed: {},
  mounted() {
    this.initTime()
    this.drawLine()
    this.initServer()
  },
  methods: {
    async initTime() {
      var dt = new Date()
      let year = dt.getFullYear()
      let month = (dt.getMonth() + 1).toString().padStart(2, '0')
      let date = dt.getDate().toString().padStart(2, '0')
      let hours = dt.getHours().toString().padStart(2, '0') //获取当前时间的小时
      let minutes = dt.getMinutes().toString().padStart(2, '0') //获取当前时间的分数
      let seconds = dt.getSeconds().toString().padStart(2, '0') //获取当前时间的秒数

      this.dateValue = [`${year}-${month}-01 00:00:00`, `${year}-${month}-${date} 23:59:59`]
    },
    initServer() {
      serverGetAll().then((resp) => {
        if (resp) {
          this.servers = resp
        }
      })
    },
    async drawLine() {
      // 基于准备好的dom，初始化echarts实例
      let myChart = this.$root.echarts.init(document.getElementById('myChart'))
      let option = {
        title: {
          text: '服务告警记录比例统计图',
          subtext: '告警记录数据',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          bottom: 'bottom',
          trigger: 'item'
        },
        toolbox: {
          show: true,
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        series: [
          {
            name: '告警记录个数',
            type: 'pie',
            radius: '55%',
            center: ['50%', '45%'],
            roseType: 'area',
            itemStyle: {
              borderRadius: 8
            },
            data: [],
            label: {
              show: true,
              formatter: '{b}：{c}个'
            }
          }
        ]
      }

      if (this.dateValue !== null && this.dateValue.length === 2) {
        let startTime = this.dateValue[0]
        let endTime = this.dateValue[1]
        this.request.startTime = startTime
        this.request.endTime = endTime
      } else {
        this.request.startTime = ''
        this.request.endTime = ''
      }

      getRecordCountByProcessStatus(this.request).then((res) => {
        res.forEach((item) => {
          option.series[0].data.push({ name: item.name, value: item.count })
        })
        // 绘制图表
        myChart.setOption(option)
        myChart.on('click', this.clickFunc)
      })
    },
    clickFunc(param) {
      let serverId
      let name = param.data.name
      this.servers.forEach((server) => {
        if (name === server.name) {
          serverId = server.id
        }
      })
      // 跳转记录页面并给过去查询参数
      this.$router.push({
        path: '/alarm/record',
        query: {
          serverId: serverId,
          startTime: this.request.startTime,
          endTime: this.request.endTime,
          processStatus: this.request.processStatus,
          level: this.request.level
        }
      })
    }
  }
}
</script>

<style></style>
